package com.infore.statisticsAndAlarm.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.infore.statisticsAndAlarm.entity.DayData;
import com.infore.statisticsAndAlarm.entity.MonthData;
import com.infore.statisticsAndAlarm.entity.QuarterData;
import com.infore.statisticsAndAlarm.entity.YearData;
import com.infore.statisticsAndAlarm.repository.IDayDataRepository;
import com.infore.statisticsAndAlarm.repository.IMonthDataRepository;
import com.infore.statisticsAndAlarm.repository.IQuarterDataRepository;
import com.infore.statisticsAndAlarm.repository.IYearDataRepository;
import com.infore.statisticsAndAlarm.service.insertTest;
import com.infore.statisticsAndAlarm.util.DateUtil;
import com.infore.statisticsAndAlarm.util.ResultEntity;

@Service
public class insertTestImpl implements insertTest {

	@Autowired
	private IDayDataRepository IDayDataRepository;
	@Autowired
	private IMonthDataRepository IMonthDataRepository;
	@Autowired
	private IQuarterDataRepository IQuarterDataRepository;
	@Autowired
	private IYearDataRepository IYearDataRepository;
	private static List<String> mns = Arrays.asList("40000000000002", "40000000000003", "40000000000004",
			"40000000000001", "A430100_2007");
	private static List<String> codes = Arrays.asList("w01010", "w01009", "w01003", "w01014", "w01019", "w21003",
			"w21011", "w21001", "w01001", "w01018");

	@Override
	public ResultEntity insert() {
		// TODO Auto-generated method stub
		// 插入日数据
		List<String> dateList = DateUtil.getTimeList("2019-09-01 00:00:00", "2020-01-01 00:00:00",
				DateUtil.DATE_SMALL_STR, "day");
		List<DayData> list = new ArrayList<DayData>();
		for (String mn : mns) {
			for (String code : codes) {
				for (String string : dateList) {
					DayData dayData = new DayData();
					dayData.setDataFlag("N");
					dayData.setDataTime(DateUtil.stringToDate(string, "yyyy-MM-dd"));
					int max = ((int) (5 + Math.random() * 20));
					int min = ((int) (1 + Math.random() * 5));
					dayData.setDataValueAvg(((max + min) / 2.0) + "");
					dayData.setDataValueMax(max + "");
					dayData.setDataValueMin(min + "");
					dayData.setFactorCode(code);
					dayData.setRealNum(((int) (1 + Math.random() * 20)) + "");
					dayData.setStandardNum("24");
					dayData.setStationCode(mn);
					dayData.setWaterQuality("1");
					list.add(dayData);
				}
			}
		}
		IDayDataRepository.saveAll(list);
		return new ResultEntity().setInfo(list.size());
	}

	@Override
	public ResultEntity insertMonth() {
		// TODO Auto-generated method stub
		List<String> dateList = DateUtil.getTimeList("2019-09", "2020-01",
				DateUtil.DATE_SMALL_YM, "month");
		List<MonthData> monthDatas = new ArrayList<MonthData>();
		for (String mn : mns) {
			for (String code : codes) {
				for (String string : dateList) {
					MonthData monthData = new MonthData();
					monthData.setDataFlag("N");
					monthData.setDataTime(DateUtil.stringToDate(string, "yyyy-MM"));
					int max = ((int) (5 + Math.random() * 20));
					int min = ((int) (1 + Math.random() * 5));
					monthData.setDataValueAvg(((max + min) / 2.0) + "");
					monthData.setDataValueMax(max + "");
					monthData.setDataValueMin(min + "");
					monthData.setFactorCode(code);
					monthData.setRealNum(((int) (1 + Math.random() * 20)) + "");
					monthData.setStandardNum("30");
					monthData.setStationCode(mn);
					monthData.setWaterQuality("1");
					monthDatas.add(monthData);
				}
			}
		}
		IMonthDataRepository.saveAll(monthDatas);
		return new ResultEntity().setInfo(monthDatas.size());
	}

	@Override
	public ResultEntity insertQuater() {
		// TODO Auto-generated method stub
		List<QuarterData> quarterDatas = new ArrayList<QuarterData>();
		for (int i = 1; i < 5; i++) {
			for (String mn : mns) {
				for (String code : codes) {
					QuarterData quarterData = new QuarterData();
					quarterData.setDataFlag("N");
					quarterData.setDataValueAvg(((int) (1 + Math.random() * 20)) + "");
					quarterData.setDataValueMax(((int) (1 + Math.random() * 20)) + "");
					quarterData.setDataValueMin(((int) (1 + Math.random() * 20)) + "");
					quarterData.setFactorCode(code);
					quarterData.setQuarter(i + "");
					quarterData.setYear("2019");
					quarterData.setRealNum(((int) (1 + Math.random() * 10)) + "");
					quarterData.setStandardNum("10");
					quarterData.setStationCode(mn);
					quarterData.setWaterQuality("1");
					quarterDatas.add(quarterData);
				}
			}
		}
		IQuarterDataRepository.saveAll(quarterDatas);
		return new ResultEntity().setInfo(quarterDatas.size());
	}

	@Override
	public ResultEntity insertYear() {
		// TODO Auto-generated method stub
		List<YearData> years = new ArrayList<YearData>();
		for (int i = 0; i < 6; i++) {
			int year = 2015 + i;
			for (String mn : mns) {
				for (String code : codes) {
					YearData yearData = new YearData();
					yearData.setDataFlag("N");
					yearData.setDataValueAvg(((int) (1 + Math.random() * 20)) + "");
					yearData.setDataValueMax(((int) (1 + Math.random() * 20)) + "");
					yearData.setDataValueMin(((int) (1 + Math.random() * 20)) + "");
					yearData.setFactorCode(code);
					yearData.setRealNum(((int) (1 + Math.random() * 12)) + "");
					yearData.setStandardNum("12");
					yearData.setStationCode(mn);
					yearData.setWaterQuality("1");
					yearData.setYear(year + "");
					years.add(yearData);
				}
			}
		}
		IYearDataRepository.saveAll(years);
		return new ResultEntity().setInfo(years.size());
	}

}
